package com.dwjm.shop.merchant;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * dwjmshop
 * com.dwjm.shop.merchant
 * 类功能描述
 *
 * @author 邓幸福
 * @date 2025/4/15 下午 04:41
 */
@WebServlet(("/addGoods"))
public class GoodsHandler extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        // 获取浏览器传递过来的参数，商品信息
        String title = req.getParameter("title"); // .trim() 去除空格
        String price = req.getParameter("price");
        String descr = req.getParameter("descr");
        String img = req.getParameter("img");
        String mid = req.getParameter("mid");
        // 封装对象
        Goods goods = new Goods(null, title, new BigDecimal(price), descr, img, "0", Integer.parseInt(mid));
        MerchantDao merchantDao = new MerchantDaoImpl();
        int i = merchantDao.addGoods(goods);
        if (1 == i) {
            resp.getWriter().write("Y");
        } else {
            resp.getWriter().write("N");
        }
    }
}
